public class GetMoneyAmount {

    //假定情况最坏，假定我最聪明!!!
    public int getMoneyAmount(int n) {
        int[][] dp = new int[n + 1][n + 1];
        for (int i = n - 1; i > 0 ; i--) {
            for (int j = i + 1; j < n + 1; j++) {
                dp[i][j] = Math.min(i + dp[i + 1][j], j + dp[i][j - 1]);
                for (int k = i + 1; k < j; k++) {
                    dp[i][j] = Math.min(dp[i][j], k + Math.max(dp[k + 1][j], dp[i][k - 1]));
                }
            }
        }
        return dp[1][n];
    }
}
